﻿using System;
using VIT.Library.Web;
using VIT.Web.Controls;
using VIT.Provider;
using System.Data;
using System.Collections.Generic;
using System.Web.UI.WebControls;

namespace VIT.Web.Admin
{
    public partial class MediaEdit : VIT.Web.UI.VITComponent
    {
        public string genOption = string.Empty;
        VIT.Provider.Media obj;

        protected void Page_Load(object sender, EventArgs e)
        {
            obj = new VIT.Provider.Media();
            try
            {
                obj.Id = int.Parse(Request["sId"]);
            }
            catch
            {
                obj.Id = -1;
            }

            if (obj.Id < 0) ButtonInsert.Visible = true; else ButtonModify.Visible = true;

            if (!IsPostBack)
            {
                loadLanguages();
                loadData();
                loadAlbum();
            }
            loadMediaType(obj.Id);
        }

        #region Methods
        private void loadData()
        {
            obj.LanguageId = Session["language"].ToString();
            obj.GetSingle();

            if (obj.Id > 0)
            {
                MediaTitle.Text = obj.Title;
                Description.Value = obj.Description;
                Tag.Text = obj.Tag;
                Orders.Text = obj.Orders.ToString();
                Published.Checked = obj.Published;
            }
        }

        private void loadMediaType(int id)
        {
            MediaType at = new MediaType();
            at.LanguageId = obj.LanguageId;
            DataTable dt = at.GetAll();
            ListItem li;

            foreach (DataRow dr in dt.Rows)
            {
                li = new ListItem(dr["Title"].ToString(), dr["Id"].ToString());
                if (id > 0) if (obj.hasType(dr["Id"].ToString())) li.Selected = true;
                MediaType.Items.Add(li);
            }

        }

        private void loadLanguages()
        {
            LanguageId.DataSource = new Language().getAll();
            LanguageId.DataTextField = "Name";
            LanguageId.DataValueField = "Id";
            LanguageId.DataBind();

            LanguageId.SelectedValue = Session["language"].ToString();
        }

        private void loadAlbum()
        {
            VIT.Provider.Album cat = new VIT.Provider.Album();
            cat.LanguageId = Session["language"].ToString();
            this.genOption = cat.GetAll().GenTagOption("Id", "Title", "0", selected: obj.AlbumId.ToString());
        }

        private void save()
        {
            var data = new Dictionary<dynamic, List<object>> {
                                                        {MediaTitle, new List<object> {"required"}},
                                                        {Orders, new List<object> {"number"}}
                                                    };
            if (MainCore.checkData(data))
            {
                bool imgvalid = true;
                if (fupFile.HasFile)
                {
                    if (MainCore.checkFile(fupFile.FileName, "isVideo"))
                    {
                        if (Filename.Text != "") obj.Filename = Filename.Text;
                        else obj.Filename = String.Format("{0:yyMMddhhmmss}", DateTime.Now) + System.IO.Path.GetExtension(fupFile.FileName);
                        fupFile.SaveAs(URLBaseDir + "Uploads/Videos/" + obj.Filename);
                    }
                    else
                    {
                        imgvalid = false;
                        setTrueVisible(lblHinhKoDung);
                    }
                }
                else obj.Filename = Filename.Text;

                if (imgvalid)
                {
                    obj.AlbumId = Convert.ToInt32(Request["AlbumId"]);
                    obj.LanguageId = LanguageId.SelectedValue;
                    obj.Title = MediaTitle.Text;
                    obj.Description = Description.Value;
                    obj.Tag = Tag.Text;
                    if (Orders.Text != "")
                        obj.Orders = int.Parse(Orders.Text);
                    obj.Published = Published.Checked;
                    //obj.Type = Request["MediaType"];                    
                    for (int i = 0; i < MediaType.Items.Count; i++)
                    {
                        if (MediaType.Items[i].Selected)
                        {
                            obj.Type += MediaType.Items[i].Value + ",";
                        }
                    }
                    if (obj.Save()) setTrueVisible(lblThanhCong);
                    else setTrueVisible(lblThatBai);
                }
            }
            else setTrueVisible(lblThatBai);
        }

        private void setTrueVisible(System.Web.UI.HtmlControls.HtmlGenericControl control)
        {
            lblThanhCong.Visible = false;
            lblThatBai.Visible = false;
            lblHinhKoDung.Visible = false;
            control.Visible = true;
        }
        #endregion

        #region
        protected void Save_Click(object sender, EventArgs e)
        {
            save();
            RedirectComponent("Media", "MediaList.aspx");
        }

        protected void Apply_Click(object sender, EventArgs e)
        {
            save();
            loadData();
            loadAlbum();
        }

        protected void Cancel_Click(object sender, EventArgs e)
        {
            RedirectComponent("Media", "MediaList");
        }

        protected void LanguageId_SelectedIndexChanged(object sender, EventArgs e)
        {
            Session["language"] = LanguageId.SelectedValue;
            loadData();
            loadAlbum();
        }
        #endregion        
    }
}
